package graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;


public class SearchInput {

	private static final int X_PREFIX = 14;
	private static final int X_SUFFIX = 28;
	private static final int CHAR_INTERVAL = 2;
	private static final int CHAR_WIDTH = 14;
	private static final char DEFAULT_CHAR = '%';
	
	private int bgColor = BootColor.BLACK;
	private int brushColor = BootColor.WHITE;

	private long RE_PAINT_INTERVAL = 500;
	private int length = 2;
	private int cursorAt = 0;
	private char[] chars = null;
	private Canvas parent = null;
	private boolean cursorVisible = false;
	private int y;
	private Timer repaintTimer;
	
	public SearchInput(Canvas aParent, int aLength) {
		length = aLength;
		parent = aParent;
		chars = new char[length];
		
		for (int i = 0;i < chars.length; i++) {
			chars[i] = DEFAULT_CHAR;
		}
		
		repaintTimer = new Timer();
		repaintTimer.schedule(new TimerTask() {

			public void run() {
				cursorVisible = !cursorVisible;
				parent.repaint();
			}}
			, RE_PAINT_INTERVAL, RE_PAINT_INTERVAL);
	}
	
	public void repaint(Graphics g) {
		g.setColor(bgColor);
		int xOffset = 3;
		int height = 24;
		int yOffset = 2;
		
		int x = determineX();
		
		g.drawRoundRect(x, y,
			CHAR_WIDTH * length
			+ CHAR_INTERVAL * length
			+ xOffset * 2,
			height + yOffset*2, 3, 3);

		g.setColor(brushColor);
		
		for (int i = 0;i < length; i++) {
//			int baseX = x + xOffset + CHAR_WIDTH * i + CHAR_INTERVAL * i;
			int baseX = x + xOffset;
			if (i > 0) {
				if (chars[i] == DEFAULT_CHAR) {
					baseX += CHAR_WIDTH + CHAR_INTERVAL;
				} else {					
					baseX += g.getFont().charWidth(Character.toUpperCase(chars[i - 1]));
				}
			}
			
			int baseY = y + yOffset;
			if ((i == cursorAt) && (cursorVisible)) {
				if (chars[i] == DEFAULT_CHAR) {					
					g.fillRect(baseX, baseY, CHAR_WIDTH, height);
				} else {
					char upperChar = toUpperchar(chars[i]);
					g.drawChar(upperChar, baseX, baseY + 1,
							Graphics.LEFT | Graphics.TOP);
				}
			} else if (i != cursorAt) {
				if (chars[i] == DEFAULT_CHAR) {
					g.fillRect(baseX, baseY, CHAR_WIDTH, height);
				} else {
					char upperChar = toUpperchar(chars[i]);
					g.drawChar(upperChar, baseX, baseY + 1,
						Graphics.LEFT | Graphics.TOP);
				}
			}
		}
	}
	
	private char toUpperchar(char c) {
		return String.valueOf(c).toUpperCase().charAt(0);
	}

	private int determineX() {
		int fromLeftBorder
		= ((240 - X_PREFIX - X_SUFFIX)
		- (length*CHAR_WIDTH + (length - 1)*CHAR_INTERVAL))
		/2;
		return X_PREFIX + fromLeftBorder;
	}

	public void charInput(char input) {
		chars[cursorAt] = input;
	}

	public int getCursor() {
		return cursorAt;
	}

	public void setCursorAt(int value) {
		secureSetCursor(value);
	}

	public void shiftCursor(int value) {
		secureSetCursor(cursorAt + value);
	}
	
	private void secureSetCursor(int value) {
		cursorAt = value;
		if (cursorAt < 0) {
			cursorAt = 0;
		} else if (cursorAt > length - 1) {
			cursorAt = length - 1;
		}
	}
	
	public void setColors(int aBgColor, int aCursorColor) {
		bgColor = aBgColor;
		brushColor = aCursorColor;
	}

	public void setCharAt(int pos, char value) {
		chars[pos] = value;
	}

	public void setY(int aY) {
		y = aY;
	}
}
